//将表单序列为对象
function getFormJson(form) {
    var o = {};
    var a = $(form).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
}

//通过字段倒序排序
function sotrObj(propertyName){
    return function(object1,object2){
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if(value1 < value2){
            return 1;
        }else if(value1 > value2){
            return -1;
        }else{
            return 0;
        }
    }
}

//计算时间差
function getTimeDifference(dateStart) {
    var showDiff = {};//返回的时间差
    // var dateStart= '2018-10-01 00:00:00';  //开始时间
    var dateEnd = new Date();    //结束时间
    var dateDiff = dateEnd.getTime() - new Date(dateStart).getTime();   //时间差的毫秒数

    //------------------------------

    //计算出相差天数
    var days=Math.floor(dateDiff/(24*3600*1000))

    //计算出小时数

    var leave1=dateDiff%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))
    //计算相差分钟数
    var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000)

    if(days>1){
        showDiff.text = days+"天前";
        showDiff.type = 1;
    }
    else if (days === 1){
        showDiff.text = "昨天";
        showDiff.type = 2;
    }
    else if (hours >= 1){
        showDiff.text = hours+"小时前"
        showDiff.type = 3;
    }
    else if (minutes >= 1){
        showDiff.text = minutes+"分钟前";
        showDiff.type = 4;
    }
    else if (minutes <= 1){
        showDiff.text = "1分钟前"
        showDiff.type = 4;
    }
    // alert(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒");
    return showDiff;
}

//绑定点击事件
var bind = function (button,fn) {
    button.onclick=function () {
        fn();
    }
};

//格式化字符串String.format()
String.prototype.format = function () {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if(args[key]!=undefined){
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题
                    var reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
};

//图片上传预览
function readURL(input) {
    if (input.files && input.files[0]) {
        var ext = input.files[0].name.split('.')[1].toLowerCase();
        var imgExt = "jpg,gif,png,jpeg";
        imgObj = $("#imgShow");
        if (imgExt.indexOf(ext) > -1) {
            var reader = new FileReader();
            reader.onload = function (e) { imgObj.attr('src', e.target.result); };
            reader.readAsDataURL(input.files[0]);
            imgObj.show();
        }
        else {
            layer.msg('请选择图片文件', {icon: "2"});
        }

    } else {
        //IE下，使用滤镜
        var docObj = document.getElementByIdx_x('doc');
        docObj.select();
        //解决IE9下document.selection拒绝访问的错误
        docObj.blur();
        var imgSrc = document.selection.createRange().text;
        // var localImagId = document.getElementByIdx_x("localImag");
        // $('#localImag').width(150).height(200); //必须设置初始大小
        //图片异常的捕捉，防止用户修改后缀来伪造图片
        try {
            localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
        } catch (e) {
            layer.msg('上传的图片格式不正确，请重新选择!', {icon: "2"}); return false;
        }
        imgObj.hide();
        document.selection.empty();
    }
}

//选择标签
$(document).on("click",".t-tag-category",function () {
    if($(this).hasClass("selected")){
        $(this).removeClass("selected");
    }
    else{
        $(this).addClass("selected");
    }
});

//代码休眠
function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

//var args = getQueryStringArgs();
//args["name"];
function getQueryStringArgs() {
    var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
        //保存数据对象
        args = {},
        //取得每一项
        items = qs.length ? qs.split('&') : [],
        item = null, name = null, value = null, i = 0, len = items.length;
    //逐个将每一项添加到args对象中
    for (i = 0; i < len; i++) {
        item = items[i].split('=');
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);
        if (name.length) {
            args[name] = value;
        }
    }
    return args;
}

//获取某年某月有多少天
function getDaysInOneMonth(year, month){
    month = parseInt(month, 10);
    var d= new Date(year, month, 0);
    return d.getDate();
}

//开始加载
var openLoad = function(){
	window.loading = layer.load(1, {
		shade: [0.3,'#000000'] //0.1透明度的白色背景
	});
} 
//关闭加载
var closeLoad =function(){
	layer.close(loading);
} 


//提示列表会为空
function tipSearchNull(id){
	var html = "<div class='full-screen-tip full-screen-tip--empty'><p>很抱歉，未找到相关课程！<br>请换个关键词试试</p></div>";
	$(id).html(html);
}


//图片懒加载
function lazyloadImg(){
	$("img.lazy-load").lazyload({
		effect : "fadeIn"
	});
}
